package iii.myangel;

import java.io.IOException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import packet.CheckLogin;

import TCP.Communication.LinkerClient;
import TCP.Communication.Role;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	public static MainActivity accCheck;
	public static LinkerClient lcl;
	private String yangman="angel";
	EditText txtID, txtPW;
	String account,pw;
	public static Handler handlerOK= new Handler();
	public static Handler handlerError= new Handler();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		InitialComponent();
		
		Thread thread = new Thread(new Runnable() {
			public void run() {
				try {
					concetion();
					
				} catch (UnknownHostException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		});
		thread.start();
		accCheck=this;
		
		
		
	}
	public void concetion() throws UnknownHostException, IOException {
		//lcl = new LinkerClient("54.199.230.253", 7777, Role.admin);
		lcl = new LinkerClient("192.168.1.143", 7777, Role.admin);
		lcl.setUserName(yangman);
	}
	
	//接收socket送回來的封包	
	public static Runnable passwordError() {
		return new Runnable() {
			@Override
			public void run() {
				accCheck.pwError();
			}
		};
	}
	public static Runnable passwordOk() {
		return new Runnable() {
			@Override
			public void run() {
				accCheck.pwOk();
				
			}
		};
	}
	
	//接收後更新地圖
	private void pwError() { 

		Toast.makeText(this, "帳密不符，請重新輸入", Toast.LENGTH_SHORT).show();		
		txtID.setText("");
		txtPW.setText("");
	}
	private void pwOk() { 
		
		
		Intent intent = new Intent(MainActivity.this,
				MainControlActivity.class);
		startActivity(intent);
		
	}
	

	OnClickListener btnLogin_click = new OnClickListener() {
		public void onClick(View arg0) {
			account=txtID.getText().toString();
			pw=txtPW.getText().toString();
			
			CheckLogin ckLo= new CheckLogin();
			ckLo.acc=account;
			ckLo.pw=pw;
			lcl.send(ckLo);
			
			


		}
	};
	OnClickListener btnAccountNew_click = new OnClickListener() {
		public void onClick(View arg0) {

			Intent intent = new Intent(MainActivity.this,
					CaregriverActivity.class);
			startActivity(intent);

		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private void InitialComponent() {
		btnLogin = (ImageButton) findViewById(R.id.btnLogin);
		btnLogin.setOnClickListener(btnLogin_click);
//		btnAccountNew = (Button) findViewById(R.id.btnAccountNew);
//		btnAccountNew.setOnClickListener(btnAccountNew_click);
		txtID=(EditText)findViewById(R.id.txtID);
		txtPW=(EditText)findViewById(R.id.txtPW);
	}

	ImageButton btnLogin = null;
	Button btnAccountNew = null;
	
	public boolean dispatchTouchEvent(MotionEvent event) {

        View v = getCurrentFocus();
        boolean ret = super.dispatchTouchEvent(event);

        if (v instanceof EditText) {
            View w = getCurrentFocus();
            int scrcoords[] = new int[2];
            w.getLocationOnScreen(scrcoords);
            float x = event.getRawX() + w.getLeft() - scrcoords[0];
            float y = event.getRawY() + w.getTop() - scrcoords[1];

            if (event.getAction() == MotionEvent.ACTION_UP
                    && (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w
                            .getBottom())) {
                InputMethodManager imm = (InputMethodManager) getSystemService(MainActivity.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getWindow().getCurrentFocus()
                        .getWindowToken(), 0);
            }
        }
        return ret;
    }

}
